package cn.csxw.openchina.ui.fragment.syntheticalFragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.Toast;

import javax.inject.Inject;

import cn.csxw.openchina.base.BaseRecAdapter;
import cn.csxw.openchina.base.BaseRefreshFragment;

import cn.csxw.openchina.dagger2.component.DaggerRecommendFragmentComponent;
import cn.csxw.openchina.dagger2.module.RecommendFragmentModule;
import cn.csxw.openchina.dagger2.presenter.RecommendFragmentPresenter;
import cn.csxw.openchina.ui.adapter.RecommendAdapter;
import cn.csxw.openchina.utils.Global;

/**
 *  综合Fragment下的 推荐Fragment
 */

public class RecommendFragment extends BaseRefreshFragment {
    private int pageIndex = 0;
    @Inject
    RecommendFragmentPresenter mRecommendFragmentPresenter;
    public RecommendAdapter mRecommendAdapter;

    @Override
    protected BaseRecAdapter getAdapter() {
        if(mRecommendAdapter==null) {
            mRecommendAdapter = new RecommendAdapter();
        }
        return mRecommendAdapter;
    }

    @Override
    public void doRefresh() {
        mRecommendAdapter.getData().clear();
        mRecommendFragmentPresenter.callNetWork(0,20,"recommend");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setOnRefreshListener(mOnRefreshListener);
        DaggerRecommendFragmentComponent
                .builder()
                .recommendFragmentModule(new RecommendFragmentModule(this))
                .build()
                .inject(this);
        Global.onNetworkStateListener(mRecommendFragmentPresenter,this);
        //TODO: CallNetWork中记得根据常数类型个数传Object对象 如:
        mRecommendFragmentPresenter.callNetWork(0,20,"recommend");
    }
    BaseRefreshFragment.OnRefreshListener mOnRefreshListener = new OnRefreshListener() {
        @Override
        public void onUpLoadMore() {
            if (Global.isNetworkReachable(getActivity())){
                pageIndex++;
                mRecommendFragmentPresenter.callNetWork(pageIndex,20,"recommend");
            }else {
                Toast.makeText(getActivity(), "网络连接异常,无法加载更多",Toast.LENGTH_SHORT).show();
            }
        }
    };
}
